home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / rcs4 / source / rcsmerge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-07  |  10.0 KB  |  301 lines

  1. /*
  2.  *                       rcsmerge operation
  3.  */
  4. #ifndef lint
  5. static char rcsid[]=
  6. "$Header: d:/rcs/rcs/rcsmerge.c 5.6 91/02/07 13:58:13 ROOT_DOS Exp $ Purdue CS";
  7. #endif
  8. /*****************************************************************************
  9.  *                       join 2 revisions with respect to a third
  10.  *****************************************************************************
  11.  */
  12.  
  13. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  14.    Distributed under license by the Free Software Foundation, Inc.
  15.  
  16. This file is part of RCS.
  17.  
  18. RCS is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 1, or (at your option)
  21. any later version.
  22.  
  23. RCS is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with RCS; see the file COPYING.  If not, write to
  30. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  
  32. Report problems and direct all questions to:
  33.  
  34.     rcs-bugs@cs.purdue.edu
  35.  
  36. */
  37.  
  38.  
  39.  
  40. /* $Log:    rcsmerge.c $
  41.  * Revision 5.6  91/02/07  13:58:13  ROOT_DOS
  42.  * Corrected misspelling in tmp dir declaration
  43.  * 
  44.  * Revision 5.5  90/07/16  21:39:57  lfk
  45.  * checked in with -k by ROOT_DOS at 91.02.07.11.45.37.
  46.  * 
  47.  * Revision 5.5  90/07/16  21:39:57  lfk
  48.  * checkin for release compilation
  49.  * 
  50.  * Revision 5.4  90/07/15  22:55:32  lfk
  51.  * Almost the end of major revison for MS-DOS version of RCS
  52.  * 
  53.  * Revision 5.3  90/07/15  20:25:43  lfk
  54.  * Most major fixes added between rev 5.1 and rev 5.5:
  55.  *     signals fixed so they work on MS-DOS
  56.  *     Added MKS arguments code so argv can be large
  57.  *     added code to handle slashes a'la Unix
  58.  *     added more file extensions to system from MS-DOS
  59.  * 
  60.  * Revision 5.2  90/07/15  11:34:31  ROOT_DOS
  61.  * DOS version of RCS 4.0 checked in for MODS
  62.  * by lfk@athena.mit.edu
  63.  * Also update to MSC 6.0
  64.  * 
  65.  * Revision 4.5  89/05/01  15:13:16  narten
  66.  * changed copyright header to reflect current distribution rules
  67.  * 
  68.  * Revision 4.4  88/11/08  12:00:47  narten
  69.  * changes from  eggert@sm.unisys.com (Paul Eggert)
  70.  * 
  71.  * Revision 4.4  88/08/09  19:13:13  eggert
  72.  * Beware merging into a readonly file.
  73.  * Beware merging a revision to itself (no change).
  74.  * Use execv(), not system(); yield exit status like diff(1)'s.
  75.  * 
  76.  * Revision 4.3  87/10/18  10:38:02  narten
  77.  * Updating version numbers. Changes relative to version 1.1 
  78.  * actually relative to 4.1
  79.  * 
  80.  * Revision 1.3  87/09/24  14:00:31  narten
  81.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  82.  * warnings)
  83.  * 
  84.  * Revision 1.2  87/03/27  14:22:36  jenkins
  85.  * Port to suns
  86.  * 
  87.  * Revision 1.1  84/01/23  14:50:36  kcs
  88.  * Initial revision
  89.  * 
  90.  * Revision 4.1  83/03/28  11:14:57  wft
  91.  * Added handling of default branch.
  92.  * 
  93.  * Revision 3.3  82/12/24  15:29:00  wft
  94.  * Added call to catchsig().
  95.  *
  96.  * Revision 3.2  82/12/10  21:32:02  wft
  97.  * Replaced getdelta() with gettree(); improved error messages.
  98.  *
  99.  * Revision 3.1  82/11/28  19:27:44  wft
  100.  * Initial revision.
  101.  *
  102.  */
  103. #include "rcsbase.h"
  104. #ifndef lint
  105. static char rcsbaseid[] = RCSBASE;
  106. #endif
  107. static char co[] = CO;
  108. static char merge[] = MERGE;
  109.  
  110. extern int  cleanup();              /* cleanup after signals                */
  111. extern char * mktempfile();         /*temporary file name generator         */
  112. extern struct hshentry * genrevs(); /*generate delta numbers                */
  113. extern int  nerror;                 /*counter for errors                    */
  114.  
  115. #ifdef MSDOS
  116. extern char *gettmpdir();
  117. char    tmpdir[NCPPN];
  118. #endif /* MSODS */
  119.  
  120. char *RCSfilename;
  121. char *workfilename;
  122. char * temp1file, * temp2file;
  123.  
  124. #ifdef MKS
  125. main( int argc, char * argv[], char * env[] )
  126. #else
  127. main( argc, argv )
  128. int argc;
  129. char *argv[];
  130. #endif /* MKS Startup Args */
  131. {
  132.         char * cmdusage;
  133.         int  revnums; /* counter for revision numbers given */
  134.         int tostdout;
  135.     int nochange;
  136.         char * rev1, * rev2; /*revision numbers*/
  137.     char commarg[revlength+3];
  138.         char numericrev[revlength];   /* holds expanded revision number     */
  139.         struct hshentry * gendeltas[hshsize];/*stores deltas to be generated*/
  140.         struct hshentry * target;
  141. #ifdef MKS
  142.     int z = 0;
  143.     int ARGC = 0;
  144.     char **tmp;
  145.     char *env_name;
  146. #    define MAXARGS 500        /* This means 500 items on the command line */
  147.     if (!(tmp = (char **)malloc(sizeof(char *)*(MAXARGS+1)))) {
  148.         fprintf(stderr, "%s: can't allocate space for arguments\n",argv[0]);
  149.         exit(1);
  150.     }
  151.     for ( z = 0 ; env[z] != NULL; z++) {    /* loop through environment */
  152.         if (*env[z] == '~') {    /* testing for entries begining with '~' */
  153.             *++env[z];            /* increment pointer to delete '~' */
  154.             tmp[ARGC++] = env[z];    /* add it to our new array */
  155.             if (z >= MAXARGS) {
  156.                 fprintf(stderr, "%s: can't handle any more arguments\n", argv[0]);
  157.                 goto list;
  158.             }
  159.         }
  160.         else if (*env[z] == '_') {    /* testing for entries begining with _ */
  161.             *++env[z] ; *++env[z];    /* move past the '_' and the '=' */
  162.             env_name = env[z];    /* copy the name */
  163.         }
  164.     }
  165. list:
  166.     if ( STREQ( (char *) argv[0] , env_name ) ) {    /* test name against startup args */
  167.         /* environment arguments meant for this program */
  168. #    ifdef DEBUG
  169.         printf("Using shell supplied args\n");
  170. #    endif
  171.         argc = ARGC;
  172.         tmp[ARGC] = NULL;    /* the terminal NULL */
  173.         argv = tmp;
  174.     }
  175. #    ifdef DEBUG
  176.     else 
  177.         printf("Using startup supplied args\n");
  178. #    endif /* debug */
  179. #endif /* MKS */
  180.  
  181.         catchints();
  182.         cmdid = "rcsmerge";
  183.         cmdusage = "command format:\n    rcsmerge -p -rrev1 -rrev2 file\n    rcsmerge -p -rrev1 file";
  184.     revnums=0;tostdout=false;nochange=false;
  185.  
  186.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  187.                 switch ((*argv)[1]) {
  188.                 case 'p':
  189.                         tostdout=true;
  190.                         /* falls into -r */
  191.                 case 'r':
  192.                         if ((*argv)[2]!='\0') {
  193.                             if (revnums==0) {
  194.                                     rev1= *argv+2; revnums=1;
  195.                             } elif (revnums==1) {
  196.                                     rev2= *argv+2; revnums=2;
  197.                             } else {
  198.                                     faterror("too many revision numbers");
  199.                             }
  200.                         } /* do nothing for empty -r or -p */
  201.                         break;
  202.  
  203.                 default:
  204.                         faterror("unknown option: %s\n%s", *argv,cmdusage);
  205.                 };
  206.         } /* end of option processing */
  207.  
  208.         if (argc<1) faterror("No input file\n%s",cmdusage);
  209.         if (revnums<1) faterror("no base revision number given");
  210.  
  211. #ifdef MSDOS
  212.     strcpy( tmpdir, (char *) gettmpdir() );
  213. #endif /* MSDOS */
  214.  
  215.         /* now handle all filenames */
  216.  
  217.         if (pairfilenames(argc,argv,true,false)==1) {
  218.  
  219.                 if (argc>2 || (argc==2&&argv[1]!=nil))
  220.                         warn("too many arguments");
  221.                 diagnose("RCS file: %s",RCSfilename);
  222.         if (!(access(workfilename,tostdout?4:6)==0))
  223.             nowork();
  224.  
  225.                 if (!trysema(RCSfilename,false)) goto end; /* give up */
  226.  
  227.                 gettree();  /* reads in the delta tree */
  228.  
  229.                 if (Head==nil) faterror("no revisions present");
  230.  
  231.  
  232.                 if (!expandsym(rev1,numericrev)) goto end;
  233.                 if (!(target=genrevs(numericrev, (char *)nil, (char *)nil, (char *)nil,gendeltas))) goto end;
  234.                 rev1=target->num;
  235.                 if (revnums==1)  /*get default for rev2 */
  236.                         rev2=Dbranch!=nil?Dbranch->num:Head->num;
  237.                 if (!expandsym(rev2,numericrev)) goto end;
  238.                 if (!(target=genrevs(numericrev, (char *)nil, (char *)nil, (char *)nil,gendeltas))) goto end;
  239.                 rev2=target->num;
  240.  
  241.         if (strcmp(rev1,rev2) == 0) {
  242.             diagnose("Merging revision %s to itself (no change)",
  243.                 rev1
  244.             );
  245.             nochange = true;
  246.             if (tostdout) {
  247.                 FILE *w = fopen(workfilename,"r");
  248.                 if (w==NULL)
  249.                     nowork();
  250.                 fastcopy(w,stdout);
  251.             }
  252.             goto end;
  253.         }
  254.  
  255. #ifdef MSDOS
  256.                 temp1file=mktempfile(tmpdir,TMPFILE1);
  257.                 temp2file=mktempfile(tmpdir,TMPFILE2);
  258. #else
  259.                 temp1file=mktempfile("/tmp/",TMPFILE1);
  260.                 temp2file=mktempfile("/tmp/",TMPFILE2);
  261. #endif /* MSDOS */
  262.  
  263.                 diagnose("retrieving revision %s",rev1);
  264.                 VOID sprintf(commarg,"-p%s",rev1);
  265.                 if (run((char*)nil,temp1file, co,"-q",commarg,RCSfilename,(char*)nil)){
  266.                         faterror("co failed");
  267.                 }
  268.                 diagnose("retrieving revision %s",rev2);
  269.                 VOID sprintf(commarg,"-p%s",rev2);
  270.                 if (run((char*)nil,temp2file, co,"-q",commarg,RCSfilename,(char*)nil)){
  271.                         faterror("co failed");
  272.                 }
  273.                 diagnose("Merging differences between %s and %s into %s%s",
  274.                          rev1, rev2, workfilename,
  275.                          tostdout?"; result to stdout":"");
  276.  
  277.                 if (
  278.               tostdout
  279. #if defined(MSDOS) && defined(MKS)
  280.             ? run((char*)nil,(char*)nil,"sh",merge,"-p",workfilename,temp1file,temp2file,workfilename,rev2,(char*)nil)
  281.             : run((char*)nil,(char*)nil,"sh",merge,     workfilename,temp1file,temp2file,workfilename,rev2,(char*)nil)) {
  282. #else
  283.             ? run((char*)nil,(char*)nil,merge,"-p",workfilename,temp1file,temp2file,workfilename,rev2,(char*)nil)
  284.             : run((char*)nil,(char*)nil,merge,     workfilename,temp1file,temp2file,workfilename,rev2,(char*)nil)) {
  285. #endif
  286.                         faterror("merge failed");
  287.                 }
  288.         }
  289.  
  290. end:
  291.         VOID cleanup();
  292.     exit(2*(nerror!=0) + nochange);
  293.  
  294. }
  295.  
  296.  
  297. nowork()
  298. {
  299.     faterror("Can't open %s",workfilename);
  300. }
  301.